package com.bang.study.day01.list;

import org.omg.CORBA.DATA_CONVERSION;

/**
 * @Auther: Bang
 * @Date: 2019/9/8 20:06
 * @Description: 自定义数组
 */
public class Array<E> {

    private E[] data;

    private int size;

    public Array() {
        data = (E[]) new Object[10];
        size = 0;
    }

    public Array(int capacity){
        data = (E[]) new Object[10];
        size = 0;
    }

    public Array(E[] arr) {
        this.data = (E[]) new Object[arr.length];
        for (int i = 0; i < arr.length; i++) {
            data[i] = arr[i];
        }
        this.size = arr.length;
    }

    // 数组是否为空
    public boolean isEmpty() {
        return size == 0;
    }

    // 获取自定义数组中元素的容量
    public int getCapacity() {
        return data.length;
    }

    // 获取自定义数组中元素的数量
    public int getSize() {
        return size;
    }

    // 向数组末尾添加元素 时间复杂度O(1)
    public void addLast(E value) {
        add(size,value);
    }

    // 向数组头添加元素
    public void addFirst(E value) {
        add(0,value);
    }

    // 在index位置插入元素
    public void add(int index,E value) {

        if (index<0||index>size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        if (size==data.length) {
            resize(data.length * 2);
        }

        for (int i = size - 1; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = value;
        size++;
    }

    // 获取index的值
    public E get(int index){
        if (index<0||index>=size) {
            throw new ArrayIndexOutOfBoundsException("数组越界");
        }
        return data[index];
    }

    public E getFirst(){
      return   get(0);
    }

    /**
     * 数据复杂度 O(1)
     * @return
     */
    public E getLast() {
        return get(size - 1);
    }

    // 修改index的值
    public void set(int index,E value){
        if (index<0||index>=size) {
            throw new ArrayIndexOutOfBoundsException("数组越界");
        }
        data[index]=value;
    }

    /**
     * 查找value的索引
     * @param value
     * @return
     */
    public int find(E value){
        for (int i = 0; i < size; i++) {
            if (data[i].equals(value)) {
                return i;
            }
        }
        return -1;
    }

    // 查找数组中是否有元素e
    public boolean contains(E e){
        for(int i = 0 ; i < size ; i ++){
            if(data[i].equals(e)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 删除元素,返回原有元素
     * @param index
     * @return
     */
    public E remove(int index){
        if (index<0||index>=size) {
            throw new ArrayIndexOutOfBoundsException();
        }
        E temp = data[index];
        for (int i = index; i < size-1; i++) {
            data[i] = data[i+1];
        }
        size--;
        data[size] = null;
        if (size == data.length / 4 && data.length / 2 != 0) {
            // 防止复杂度震荡(缩容时太过激进),在size=capacity/4时,将数组缩容为capacity/2
            resize(data.length / 2);
        }
        return temp;
    }

    public E removeFirst(){
       return remove(0);
    }

    /**
     *  数据复杂度 O(1)
     * @return
     */
    public E removeLast(){
        return remove(size-1);
    }


    // 扩缩容
    private void resize(int capacity) {
        E[] dataEle = (E[]) new Object[capacity];
        for (int i = 0; i <size; i++) {
            dataEle[i] = data[i];
        }
        data = dataEle;
    }



    @Override
    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append("[");
        for (int i = 0; i < size; i++) {
            sb.append(i);
            if (i<size-1) {
                sb.append(",");
            }
        }
        sb.append("]");
        return "Array{" +
                "data=" + sb.toString() +
                ", size=" + size +
                ", capacity=" + data.length +
                '}';
    }

    /**
     * 交换元素位置
     * @param i
     * @param j
     */
    public void swap(int i, int j) {
        E temp = data[i];
        data[i] = data[j];
        data[j] = temp;
    }
}
